package common.components.smileyenhanced
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import mx.core.BitmapAsset;
	import mx.core.Container;

	public class Smiley extends Sprite
	{
		private var myBitmap:BitmapAsset;
		private var myPosition:int;
		private var myField:TextField;
		private var myParent:ISmileyTextEditable;
		private var myText:String;
		private var repl:String = "  十  ";
		
		private var _textLen:int;
		private var _scrollV:int;
		private var _caretIndex:int;
		
		public var killed:Boolean;
		
		public function Smiley(source:Class, field:TextField, parent:ISmileyTextEditable, pos:int, val:String)
		{
			super();
			this.myBitmap = BitmapAsset(new source());
			this.myPosition = pos;
			this.myField = field;
			this.myParent = parent;
			this.myText = val;
			
			this.killed = false;
			
			var newText:String = field.text.substring(0,pos)+this.repl+field.text.substring(pos+val.length);
			field.text = newText;
			field.setSelection(pos+this.repl.length,pos+this.repl.length);
			
			this._textLen = this.myField.text.length;
			this._scrollV = this.myField.scrollV;
			this._caretIndex = this.myField.caretIndex;
			
			this.addChild(this.myBitmap);
			this.setPosition();	
			this.myParent.addChild(this);
//			this.myParent.addEventListener("textInput",handleChange);
//			this.myParent.addEventListener("change", handleChange);
			this.myField.addEventListener("scroll", handleScroll);
			this.myParent.addEventListener("enterFrame", handleEnterFrame);
		}
		
		public function get position ():int 
		{
			return this.myPosition;
		}
		
		public function get text ():String 
		{
			return myText;
		}
		
		public function get replaceLength ():int 
		{
			return this.repl.length;
		}
		
		public function setPosition ():void 
		{
			var rct:Rectangle = this.myField.getCharBoundaries(this.myPosition+2); // zhong
			if (rct!=null)
			{
				// special hack. border style导致图标错位 
				
				if ( SmileyTextArea(this.myParent).borderMetrics.left > 0 )
					this.x = rct.left-this.myField.scrollH+(SmileyTextArea(this.myParent).borderMetrics.left-2);
				else
					this.x = rct.left-this.myField.scrollH-2;
				
//				var pt:Point = this.myField.localToGlobal(new Point(x,0));
//				
//				pt = UIComponent(this.myParent).globalToLocal(pt);
//				
//				this.x = pt.x;
//				
//				trace('x:'+pt.x);
				
				this.y = rct.top-2;
				var sp:int = this.myField.scrollV-1;
				this._scrollV = this.myField.scrollV;
				this.y -= sp*this.myParent.getLineMetrics(0).height;
				this.checkVisibility();
			} 
			else 
			{
//				return;
				this.visible = false;
			}
		}
		
		public function handleChange(evt:Event=null):void 
		{
			if(this.myField.caretIndex-this.repl.length+1 == this.myPosition && 
					this.myField.text.length<this._textLen)
			{
				this.myField.text = this.myField.text.substr(0,this.myPosition)+
									this.myText.substring(0,this.myText.length-1)+
									this.myField.text.substr(this.myPosition+this.repl.length-1);
				this.myField.setSelection(this.myPosition+1,this.myPosition+1);
				this.killSelf();
			}
			else if(this.myField.caretIndex == this.myPosition && 
					this.myField.text.length<this._textLen)
			{
				this.myField.text = this.myField.text.substr(0,this.myPosition)+
									this.myText.substring(1,this.myText.length)+
									this.myField.text.substr(this.myPosition+this.repl.length-1);
				this.myField.setSelection(this.myPosition,this.myPosition);
				this.killSelf();
			}
			else if((this.myField.caretIndex) <= (this.myPosition+this.repl.length) && 
				(this.myField.selectionEndIndex) <= (this.myPosition+this.repl.length) &&
				(this.myField.text.length > this._textLen))				
			{
				this.myPosition += this.myField.text.length - this._textLen;
				this.setPosition();
			} 
			else if((this.myField.caretIndex) < (this.myPosition+this.repl.length) && 
				(this.myField.selectionEndIndex) < (this.myPosition+this.repl.length))				
			{
				this.myPosition += this.myField.text.length - this._textLen;
				this.setPosition();
			}
			
			this._textLen = this.myField.text.length;
			if(this.myPosition < 0 ) this.killSelf();
		}
		
		private function handleScroll(evt:Event):void
		{
			this.setPosition();
		}
		
		private function checkVisibility():void 
		{
//			return;
//			if ( this.parent )
//				trace(this.x+'  '+this.parent.width+'   '+this.width);
			
//			if( this.x>=0 && this.x< this.myParent.width-this.width && this.y>=0 && this.y<=this.myParent.height-this.height )
			
			// just limit the height. width is not accurate...let me think over.
			if( this.y>=0 && this.y<=this.myParent.height-this.height )
			{
				this.visible = true;
			} 
			else 
			{
				this.visible = false;
			}
		}
		
		private function handleEnterFrame (evt:Event):void 
		{	
			if(this._scrollV != this.myField.scrollV) 
			{
				this.checkVisibility();
				this.setPosition();
			}
			
			if(this._caretIndex != this.myField.caretIndex) 
			{
				var curCaret:int = this.myField.caretIndex;
				if(curCaret>this.myPosition&&curCaret<this.myPosition+this.repl.length){
					if(curCaret>this._caretIndex){
						this.myField.setSelection(this.myPosition+this.repl.length,this.myPosition+this.repl.length);
					} else {
						this.myField.setSelection(this.myPosition,this.myPosition);
					}
				}
				
				this._caretIndex = curCaret;
			}
		}
		
		public function killSelf ():void 
		{
			this.killed = true;
			this.myParent.removeSmily(this);
//			this.myParent.removeEventListener("textInput", handleChange);
//			this.myParent.removeEventListener("change", handleChange);
			this.myField.removeEventListener("scroll", handleScroll);
			this.myParent.removeEventListener("enterFrame", handleEnterFrame);
		}
		
	}
}